package com.ihisomp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ihisomp.base.Page;
import com.ihisomp.base.Result;
import com.ihisomp.dto.SelectListDto;
import com.ihisomp.dto.SysDictionaryDto;
import com.ihisomp.entity.SysDictionary;
import com.ihisomp.mapper.SysDictionaryMapper;
import com.ihisomp.service.ISysDictionaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * <p>
 * 系统公用字典 服务实现类
 * </p>
 *
 * @author zhangyong
 * @since 2021-12-21
 */
@Service
public class SysDictionaryServiceImpl extends ServiceImpl<SysDictionaryMapper, SysDictionary> implements ISysDictionaryService {

    /**
     * 根据分类查询数据列表
     *
     * @param parentDictionaryKey
     * @return
     */
    @Override
    public Result selectList(String parentDictionaryKey) {
        List<SelectListDto> list = baseMapper.selectDataList(parentDictionaryKey);
        return Result.ok(list);
    }

    /**
     * 新增数据
     *
     * @param sysDictionaryDto
     * @return
     */
    @Override
    public Result add(SysDictionaryDto sysDictionaryDto) {
        if (sysDictionaryDto == null || StringUtils.isBlank(sysDictionaryDto.getDictionaryKey())) {
            return Result.error("字典编码不能为空！");
        }
        if (StringUtils.isBlank(sysDictionaryDto.getDictionaryName())) {
            return Result.error("字典名称不能为空！");
        }
        if (baseMapper.selectById(sysDictionaryDto.getDictionaryKey()) != null) {
            return Result.error("字典数据已经存在！");
        }
        SysDictionary sysDictionary = new SysDictionary();
        BeanUtils.copyProperties(sysDictionaryDto, sysDictionary);
        if (baseMapper.insert(sysDictionary) > 0) {
            return Result.ok();
        }
        return Result.error("新增字典数据失败！");
    }

    /**
     * 删除数据
     *
     * @param dictionaryKey
     * @return
     */
    @Override
    public Result delete(String dictionaryKey) {
        //  查询条件
        QueryWrapper<SysDictionary> query = new QueryWrapper<>();
        query.like("parent_dictionary_key", dictionaryKey);
        if (baseMapper.selectList(query).size() > 0) {
            return Result.error("该字典数据存在数据源内容，请先删除节点数据源！");
        }
        if (baseMapper.deleteById(dictionaryKey) > 0) {
            return Result.ok();
        }
        return Result.error("删除字典数据失败！");
    }

    /**
     * 更新数据
     *
     * @param dictionaryKey
     * @param sysDictionaryDto
     * @return
     */
    @Override
    public Result update(String dictionaryKey, SysDictionaryDto sysDictionaryDto) {
        if (sysDictionaryDto.getDictionaryKey() == null) {
            sysDictionaryDto.setDictionaryKey(dictionaryKey);
        }
        SysDictionary sysDictionary = new SysDictionary();
        BeanUtils.copyProperties(sysDictionaryDto, sysDictionary);
        if (baseMapper.updateById(sysDictionary) > 0) {
            return Result.ok();
        }
        return Result.error("更新字典数据失败！");
    }

    /**
     * 查询数据
     *
     * @param dictionaryKey
     * @return
     */
    @Override
    public Result get(String dictionaryKey) {
        return Result.ok(baseMapper.selectById(dictionaryKey));
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @param sysDictionaryDto
     * @return
     */
    @Override
    public Result search(long page, long size, SysDictionaryDto sysDictionaryDto) {
        if (sysDictionaryDto == null) {
            sysDictionaryDto = new SysDictionaryDto();
        }
        // 分页查询
        IPage<SysDictionary> data = baseMapper.searchPage(new Page<SysDictionary>(page, size), sysDictionaryDto);
        return Result.ok(data);
    }

    /**
     * 查询数据元名称列表
     *
     * @return
     */
    @Override
    public Result selectParentDataList() {
        List<SelectListDto> list = baseMapper.selectParentDataList();
        SelectListDto dto = new SelectListDto();
        dto.setCode("");
        dto.setText("");
        list.add(0, dto);
        return Result.ok(list);
    }
}
